package bottrust;

/*
Problem

Blue and Orange are friendly robots. An evil computer mastermind has locked them up in separate hallways to test them, and then possibly give them cake.

Each hallway contains 100 buttons labeled with the positive integers {1, 2, ..., 100}. Button k is always k meters from the start of the hallway, and the robots both begin at button 1. Over the period of one second, a robot can walk one meter in either direction, or it can press the button at its position. To complete the test, the robots need to push a certain sequence of buttons in a certain order. Both robots know the full sequence in advance. How fast can they complete it?

For example, let's consider the following button sequence:

   O 2, B 1, B 2, O 4

Here, O 2 means button 2 in Orange's hallway, B 1 means button 1 in Blue's hallway, and so on. The robots can push this sequence of buttons in 6 seconds using the strategy shown below:

Time | Orange           | Blue
-----+------------------+-----------------
  1  | Move to button 2 | Stay at button 1
  2  | Push button 2    | Stay at button 1
  3  | Move to button 3 | Push button 1
  4  | Move to button 4 | Move to button 2
  5  | Stay at button 4 | Push button 2
  6  | Push button 4    | Stay at button 2
Note that Blue has to wait until Orange has completely finished pushing O 2 before it can start pushing B 1.
Input

The first line of the input gives the number of test cases, T. T test cases follow.

Each test case consists of a single line beginning with a positive integer N, representing the number of buttons that need to be pressed. This is followed by N terms of the form "Ri Pi" where Ri is a robot color (always 'O' or 'B'), and Pi is a button position.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the minimum number of seconds required for the robots to push the given buttons, in order.

Limits

1 ≤ Pi ≤ 100 for all i.

Small dataset

1 ≤ T ≤ 20.
1 ≤ N ≤ 10.

Large dataset

1 ≤ T ≤ 100.
1 ≤ N ≤ 100.

Sample


Input 
 	
Output 
 
3
4 O 2 B 1 B 2 O 4
3 O 5 O 8 B 100
2 B 2 B 1
 
 */
import java.io.File;
import java.io.PrintStream;

import support.MasterFileReader;
import support.UnhandledBehaviourException;

public class Main {
//	public Object publicObject;
	public MasterFileReader in;
	public static void main(String[] args) 
	{
		try {
			Main m=new Main();
			//get input Reader
			m.in=new MasterFileReader(args[0]);
			//get first object / and translate it
			int n=Integer.parseInt(m.in.readLine());
			
			//set output stream
			try {
				//first of all delete old file
				File f1 = new File("res.out");
				f1.delete();
				
				System.setOut(new PrintStream(new File("res.out")));
				} catch( Exception e) {
					throw new UnhandledBehaviourException("can't create Print Stream", e); }
			//
			//
			for(int i=0;i<n;i++)
			{
				String l=m.in.readLine();
				m.processTask(i+1, Integer.parseInt(l.split("\\s+")[0]), l.substring(l.split("\\s+")[0].length()+1));
				if (i < n-1)
					System.out.print("\n");
			}
	
			
		} catch(UnhandledBehaviourException e)
		{
			e.printStackTrace();
			System.out.println("\nfailed to execute, cause: "+e.exp);
			System.out.println("\nReason for fail: "+e.cause.toString()+"~"+e.cause.getLocalizedMessage());
		} catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("\nfailed to execute, cause: "+e.getLocalizedMessage());
		}
	}
	public void processTask(int testCase, int n, String sequence)
	{
		int orangeIndex=0;
		int blueIndex=0;
		int orangeLocation=1;
		int blueLocation=1;
		Action[] seq=getsequence(sequence,n);
		int i=0;
		int t=0;
		System.out.print("Case #"+testCase+": ");
		for(;orangeIndex < n || blueIndex <n; t++)
		{
			//forward them to correct location in sequence
			while( orangeIndex < n && !seq[orangeIndex].isOrange())
				orangeIndex++;
			while(blueIndex < n && seq[blueIndex].isOrange())
				blueIndex++;
			
			//	orange in place							orange is next in line
			if(orangeIndex < n && orangeLocation == seq[orangeIndex].button && i == orangeIndex)
			{
				orangeIndex++; // this part is done, he pressed the button
				i++;
				
				//check if blue needs to move
				if(blueIndex < n)
				if(blueLocation < seq[blueIndex].button)
					blueLocation++;
				else if(blueLocation > seq[blueIndex].button)
					blueLocation--;
			}
			else 
			{
				//orange movement
				if(orangeIndex < n)
				if(orangeLocation < seq[orangeIndex].button)
					orangeLocation++;
				else if(orangeLocation > seq[orangeIndex].button)
					orangeLocation--;
				
				//blue submit
				if(blueIndex < n)
				if(blueLocation == seq[blueIndex].button && i == blueIndex)
					{ 
						blueIndex++;
						i++;
					}
				//blue movement
				else if(blueLocation < seq[blueIndex].button)
					blueLocation++;
				else if(blueLocation > seq[blueIndex].button)
					blueLocation--;
			}
			
		}
		System.out.print(t);

	}
	public Action[] getsequence(String seq, int n)
	{
		String[] arg=seq.split("\\s+");
		Action[] rez=new Action[n];
		for(int i=0,j=0;i<n;i++)
			rez[i]=new Action(arg[j++],Integer.parseInt(arg[j++]) );
		return rez;
	}
	class Action
	{
		public String color;
		public int button;
		
		public Action(String s, int b)
		{
			color=s;
			button=b;
		}
		public boolean isOrange()
		{
			if(color.toUpperCase().equals("O"))
				return true;
			return false;
		}
		
	}
}
